﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;


public partial class ManagePageContents : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_PAGES = "__PageContent::DSPGS";
    private const string PROP_LAYOUT = "__PageContent::UIL";
    private const string PROP_SELECTED_PAGE_ID = "__PageContent::SUID";

    #endregion

    #region Public Properties

    public PageContentsCollection DataSourcePageContent
    {
        get { return (SessionMgr.Session[PROP_DATASRC_PAGES] is PageContentsCollection ? (PageContentsCollection)SessionMgr.Session[PROP_DATASRC_PAGES] : (PageContentsCollection)(SessionMgr.Session[PROP_DATASRC_PAGES] = new PageContentsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_PAGES] = value; }
    }

    public int SelectedPageID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_PAGE_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_PAGE_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_PAGE_ID] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourcePageContent = PageContents.GetAllPageContents();
            PageContentList1.DataBind(DataSourcePageContent);
        }
    }

    #endregion

    #region Control: PageContentList

    protected void ucPageList_EditRequested(object sender, Controls_PageContentList.EditRequestedEventArgs evt)
    {
        SelectedPageID = evt.PageC.PageID;
        PageContentViewEdit1.DataBind(evt.PageC);
        SetLayout(LayoutType.Edit);
    }

    protected void ucPageList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourcePageContent = PageContents.GetAllPageContents();
        PageContentList1.DataSource = DataSourcePageContent;
    }

    #endregion

    #region Control: PageContentViewEdit

    protected void ucPageViewEdit_DataSourcePageRequested(object sender, EventArgs evt)
    {
        PageContentViewEdit1.DataSourcePageContent = DataSourcePageContent.FindByPageID(SelectedPageID);
    }

    #endregion

    #region Control: Update / Cancel Buttons

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {
        if (PageContentViewEdit1.EndEdit())
        {
            PageContents obj = DataSourcePageContent.FindByPageID(SelectedPageID);
            PageContents.Update(obj);

            // TODO: Set Feedback text...

            PageContentList1.DataBind(DataSourcePageContent);
            SetLayout(LayoutType.View);

        }

    }
    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        PageContentViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourcePageContent.FindByPageID(SelectedPageID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourcePageContent.Remove(DataSourcePageContent.FindByPageID(SelectedPageID));
                SelectedPageID = (DataSourcePageContent[0] != null ? DataSourcePageContent[0].PageID : 0);
            }
            catch
            {
            }
        }
        PageContentList1.DataBind(DataSourcePageContent);
        SetLayout(LayoutType.View);
    }

    #endregion

    #endregion

    #region Privates

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlgridlist.Visible = false;
                pnlViewUpdate.Visible = true;
                pnlbuttons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlgridlist.Visible = true;
                pnlViewUpdate.Visible = false;
                pnlbuttons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}

